package com.hbter630.change.utils;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: Result
 * @Author 笑霸fianl
 * @Package com.hbter630.change.utils
 * @Date 2023/5/23 8:45
 * @描述: 统一返回结果
 */
@Data
public class Result<T> {
    @ApiModelProperty(value = "是否成功")
    private Boolean success;

    @ApiModelProperty(value = "状态码")
    private Integer code;

    @ApiModelProperty(value = "消息")
    private String message;

    @ApiModelProperty(value = "返回的数据")
    private T data;

    @ApiModelProperty(value = "动态数据")
    private Map map = new HashMap();

    /**
     * 防止直接new
     */
    private Result(){};

    /**
     * 成功的静态方法
     */
    public static<T> Result<T> ok(  ){
        Result r=new Result();
        r.success=true;
        r.code=ResultCode.SUCCESS;
        r.message="成功";
        return r;
    }

    /**
     * 失败的静态方法
     */
    public static Result failed(){
        Result r=new Result();
        r.success=false;
        r.code=ResultCode.ERROR;
        r.message="失败";
        return r;
    }
    public static Result failed(String message){
        Result r=new Result();
        r.success=false;
        r.code=ResultCode.ERROR;
        r.message=message;
        return r;
    }

    public Result addCode(Integer code){
        this.setCode(code);
        return this;
    }
    public Result addMessage(String message){
        this.setMessage(message);
        return this;
    }
    public Result<T> addData(T data){
        this.setData(data);
        return this;
    }

    /**
     * 用来操作 map = new HashMap(); 动态数据
     * @param key
     * @param value
     * @return
     */
    public Result<T> add(String key, Object value) {
        this.map.put(key, value);
        return this;
    }

}
